# coding:utf-8
from bs4 import BeautifulSoup

class HtmlParser(object):
    def __init__(self):
        pass
    
    def parser_page(self, content):
        soup = BeautifulSoup(content)
        url_count = self.get_url_count(soup)
        page_data = self.get_page_data(soup)
        return url_count,page_data
    
    def get_url_count(self, soup):
        page_numbers = soup.find_all('span',class_='page-numbers')
        last_span = page_numbers[len(page_numbers) - 1]
        return last_span.get_text()
    
    def get_page_data(self, soup):
        data = list()
        nodes = soup.find_all('div',class_='article block untagged mb15')
        for node in nodes:
            thumb_node = node.find('div',class_='thumb')
            if not thumb_node:
                one_node = dict()
                author_node = node.find('div',class_='author clearfix').get_text()
                one_node['name'] = author_node
                content_node = node.find('div',class_='content').get_text()
                one_node['content'] = content_node
                cm_node = node.find_all('i',class_='number')
                funny = cm_node[0].get_text()
                review = cm_node[1].get_text()
                one_node['funny'] = funny
                one_node['review'] = review
                data.append(one_node)
        return data
    